/**
 * Copyright (C) 2009+ Spark Zheng
 *
 * @file        src/prot_common.c 
 * @date        2010-04-09
 * @brief	general_network
 *		the common function called at reactors/protocols
 * @see         dserver
 * @history
 */

#include "dserver_log.h"
#include "dserver_errno.h"
#include "connection.h"
#include "prot_types.h"

/** TCP */
int dserver_tcp_conn_receive(prot_node_t *prot)
{
	if (prot == NULL) {
		return -E_ARGS_ILLEGAL;
	}

	event_set(&prot->p_event, prot->p_sfd, EV_READ | EV_PERSIST,
			prot->p_ops.p_server_conn_handler, (void *)prot);
	(void)event_base_set(prot->p_main_event, &prot->p_event);
	if (event_add(&prot->p_event, 0) < 0) {
#ifdef DSERVER_NETWORK_NEED_DLOG
		DLOG_ERR(LOG_MAIN, "protocol[%s:%d] add conn event error.",
				prot->p_name, prot->p_sfd);
#endif
		return -E_EVENT;
	}

	return RET_SUCCESS;
}

int dserver_tcp_data_sendout(dserver_conn_t *conn)
{
	prot_node_t *prot;

	if (conn == NULL || conn->c_prot_node == NULL) {
		return -E_ARGS_ILLEGAL;
	}
	prot = conn->c_prot_node;

	/*
	struct timeval tv;
	tv.tv_sec = prot->p_settings.p_timeout;
	tv.tv_usec = 0;
	event_del(&conn->c_event);
	event_set(&conn->c_event, conn->c_cfd, EV_WRITE,
			prot->p_ops.p_server_data_maker, (void *)conn);
	(void)event_base_set(prot->p_main_event, &conn->c_event);
	if (event_add(&conn->c_event, &tv) < 0) {
#ifdef DSERVER_NETWORK_NEED_DLOG
		DLOG_ERR(LOG_MAIN, "protocol[%s:%d] add write event error.",
				prot->p_name, conn->c_cfd);
#endif
		return -E_EVENT;
	}
	*/

	(*prot->p_ops.p_server_data_maker)(conn->c_cfd, EV_WRITE, (void *)conn);

	return RET_SUCCESS;
}

int dserver_tcp_data_receive(dserver_conn_t *conn)
{
	prot_node_t *prot;
	struct timeval tv;

	if (conn == NULL || conn->c_prot_node == NULL) {
		return -E_ARGS_ILLEGAL;
	}
	prot = conn->c_prot_node;

	/** add write-event to eventpool */
	tv.tv_sec = prot->p_settings.p_timeout;
	tv.tv_usec = 0;
	event_del(&conn->c_event);
	event_set(&conn->c_event, conn->c_cfd, EV_READ,
			prot->p_ops.p_server_data_handler, (void *)conn);
	(void)event_base_set(prot->p_main_event, &conn->c_event);
	if (event_add(&conn->c_event, &tv) < 0) {
#ifdef DSERVER_NETWORK_NEED_DLOG
		DLOG_ERR(LOG_MAIN, "protocol[%s:%d] add read event error.",
				prot->p_name, conn->c_cfd);
#endif
		return -E_EVENT;
	}

	return RET_SUCCESS;
}

/** UDP */
int dserver_udp_data_sendout(prot_node_t *prot)
{
	if (prot == NULL) {
		return -E_ARGS_ILLEGAL;
	}

	(*prot->p_ops.p_server_data_maker)(prot->p_sfd, EV_WRITE, (void *)prot);

	return RET_SUCCESS;
}

int dserver_udp_data_receive(prot_node_t *prot)
{
	if (prot == NULL) {
		return -E_ARGS_ILLEGAL;
	}

	event_set(&prot->p_event, prot->p_sfd, EV_READ | EV_PERSIST,
			prot->p_ops.p_server_data_handler, (void *)prot);
	(void)event_base_set(prot->p_main_event, &prot->p_event);
	if (event_add(&prot->p_event, 0) < 0) {
#ifdef DSERVER_NETWORK_NEED_DLOG
		DLOG_ERR(LOG_MAIN, "protocol[%s:%d] add read event error.",
				prot->p_name, prot->p_sfd);
#endif
		return -E_EVENT;
	}

	return RET_SUCCESS;
}

